# coding: utf-8
#!/usr/bin/python
import wx
import os

import jsonManage


class MainWindow(wx.Frame):

    def __init__(self, parent, title):
        self.dirname = ''
        wx.Frame.__init__(self, parent, title=title, size=(200, 400))
        self.control = wx.TextCtrl(self, style=wx.TE_MULTILINE)
        self.CreateStatusBar()
        filemenu = wx.Menu()

        menuOpen = filemenu.Append(wx.ID_OPEN, "&打开", "打开文件夹")
        filemenu.AppendSeparator()
        menuAbout = filemenu.Append(wx.ID_ABOUT, "&关于", "程序信息")
        filemenu.AppendSeparator()
        menuExit = filemenu.Append(wx.ID_EXIT, "&退出", "退出程序")

        menuBar = wx.MenuBar()
        menuBar.Append(filemenu, "&文件")  # Adding the "filemenu" to the MenuBar
        self.SetMenuBar(menuBar)  # Adding the MenuBar to the Frame content.

        self.Bind(wx.EVT_MENU, self.OnAbout, menuAbout)
        self.Bind(wx.EVT_MENU, self.OnExit, menuExit)
        self.Bind(wx.EVT_MENU, self.OnOpen, menuOpen)

        # self.sizer2 = wx.BoxSizer(wx.HORIZONTAL)
        # self.buttons = []
        # for i in range(0, 6):
        #     self.buttons.append(wx.Button(self, -1, "Button &" + str(i)))
        #     self.sizer2.Add(self.buttons[i], 1, wx.EXPAND)

        # Use some sizers to see layout options
        # self.sizer = wx.BoxSizer(wx.VERTICAL)
        # self.sizer.Add(self.control, 1, wx.EXPAND)
        # self.sizer.Add(self.sizer2, 0, wx.EXPAND)

        #Layout sizers
        # self.SetSizer(self.sizer)
        # self.SetAutoLayout(1)
        # self.sizer.Fit(self)
        self.Show()

    def OnAbout(self, e):
        # A message dialog box with an OK button. wx.OK is a standard ID in wxWidgets.
        dlg = wx.MessageDialog(self, "A small text editor",
                               "About Sample Editor", wx.OK)
        dlg.ShowModal()  # Show it
        dlg.Destroy()  # finally destroy it when finished.

    def OnExit(self, e):
        self.Close(True)  # Close the frame.

    def OnOpen(self, e):
        """ Open a file"""
        dlg = wx.FileDialog(self, "Choose a file", self.dirname, "", "*.*",
                            wx.FD_OPEN)
        if dlg.ShowModal() == wx.ID_OK:
            jsontext = jsonManage.loadJsonFile2(
                os.path.join(dlg.GetDirectory(), dlg.GetFilename()))
            print(jsontext)
            self.control.SetValue("jsontext")
            # self.filename = dlg.GetFilename()
            # self.dirname = dlg.GetDirectory()
            # f = open(os.path.join(self.dirname, self.filename), 'r')
            # self.control.SetValue(f.read())
            # f.close()
        dlg.Destroy()


app = wx.App(False)
frame = MainWindow(None, 'GuiGuBaHuang Villain_Editer')
app.MainLoop()